package com.black.utils.proxy;

import com.black.utils.proxy.handler.AbstractChainHandler;
import net.sf.cglib.proxy.Enhancer;

/**
 * @author ylx
 */
public class ProxyUtil {

    /**
     * 获取代理对象
     *
     * @param target   目标类
     * @param handler 链式代理处理器
     * @param <T>      目标类类型
     * @return 代理对象
     */
    public static <T> T getProxy(Class<T> target, AbstractChainHandler handler) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target);
        enhancer.setCallback(handler);
        Object o = enhancer.create();
        return target.cast(o);
    }
}
